<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表格Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>修改新闻</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="example" method="POST" action="" style="padding: 10px 50px;">

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">新闻编号:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" name="newsId" disabled="true" autocomplete="off" id="newsId" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">新闻标题</label>
            <div class="layui-input-block">
                <input type="text" name="newsTitle" id="newsTitle" lay-verify="required" placeholder="请输入" required
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">媒体类型</label>
            <div class="layui-input-block">
                <select name="mediaId" id='mediaId'></select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">阅读人数</label>
            <div class="layui-input-block">
                <input type="text" name="newsReading" id="newsReading" lay-verify="required" placeholder="请输入" required
                    autocomplete="off" class="layui-input" type="number">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收藏人数</label>
            <div class="layui-input-block">
                <input type="text" name="newsCollection" id="newsCollection" lay-verify="required" placeholder="请输入"
                    required autocomplete="off" class="layui-input" type="number">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">新闻时间</label>
            <div class="layui-input-block">
                <input name="newsTime" id="newsTime" required lay-verify="required" placeholder="请输入" required
                    class="layui-input"></input>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">新闻内容</label>
            <div class="layui-input-block">
                <textarea name="newsContent" id="newsContent" required lay-verify="required" placeholder="请输入"
                    class="layui-textarea"></textarea>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="uploadBtn">选择图片</button>
                <div class="layui-upload-list" id="previewBox">

                </div>
            </div>
        </div>




        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit" lay-submit="" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <style>
        .layui-upload-list {
            padding: 10px 40px;
            display: flex;
            min-height: 150px;
            min-width: 150px;
            border: 1px solid #cccccc;
        }

        .imgitem {
            cursor: pointer;
            background-color: #FF5722;
            ;
            width: 100%;
            text-align: center;
        }

        .imgBox {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin: 0px 20px;
        }
    </style>
    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>
    <script src="../js/util.js" charset="utf-8"></script>
    <script>
        layui.config({
            base: '/module/'
        }).use(['jquery', 'layer', 'table', 'form', 'config', 'laydate', 'upload', 'rate'], function () {

            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var upload = layui.upload;
            var config = layui.config;
            var rate = layui.rate;
            var laydate = layui.laydate;
            var srcArr = new Array();
            var imgUrl = ''
            var files = [];
            var req = GetRequest();
            var no = req['editNo'];

            $.ajax({
                url: config.base_server + 'GetnewsById?newsId=' + no,
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    data = data.data;
                    $('#newsId').val(data[0].newsId);
                    $('#newsTitle').val(data[0].newsTitle);
                    $('#newsReading').val(data[0].newsReading);
                    $('#newsCollection').val(data[0].newsCollection);

                    srcArr =data[0].newsImgSrc;
                    srcArr.forEach((item, index) => {
                        $('#previewBox').append('<div class="imgBox" ><img src="' + item + '" alt="" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>');
                        $('.remove_' + index).bind('click', function () {
                            let itemIndex = $(this).parent().parent().find('.imgBox').index($(this).parent()[0]);
                            srcArr.splice(itemIndex, 1);
                            $(this).parent().remove();
                        })
                    })

                    $('#newsContent').val(data[0].newsContent);
                    $('#newsTime').val(data[0].newsTime)
                }
            })


            upload.render({
                elem: '#uploadBtn'
                , url: '/UpdateSaler'
                , multiple: true
                , bindAction: "#save"
                , auto: false
                , choose: (obj) => {//预览图片
                    files = obj.pushFile();
                    obj.preview((index, file, result) => {
                        imgUrl = result
                        $('#previewBox').append('<div class="imgBox" ><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>')
                        $('.remove_' + index).bind('click', function () {
                            delete files[index];//删除指定图片
                            $(this).parent().remove();
                        })
                    });
                }
            });


            laydate.render({
                elem: '#newsTime',
                value: new Date()
                , type: 'datetime'
                , trigger: 'click'
            });

            $.ajax({
                url: config.base_server + 'getmediaitiveAll',
                dataType: 'json',
                type: 'get',
                async: false,
                success: function (data) {
                    data = data.data;
                    $.each(data, function (index, item) {
                        $('#mediaId').append(new Option(item.mediaAuthor, item.mediaId));
                    });
                    form.render("select");
                }
            })


            $('#submit').on('click', function () {
                var data = form.val('example');
                var newsId = data.newsId;
                var newsTitle = data.newsTitle;
                var newsContent = data.newsContent;
                var mediaId = data.mediaId;
                var newsReading = data.newsReading;
                var newsCollection = data.newsCollection;
                var newsTime = data.newsTime;
                var formData = new FormData();

                formData.append('newsId', newsId);
                formData.append('newsTitle', newsTitle);
                formData.append('newsContent', newsContent);
                formData.append('mediaId', mediaId);
                formData.append('newsReading', newsReading);
                formData.append('newsCollection', newsCollection);
                formData.append('newsTime', newsTime);
                for (var i = 0; i < Object.values(files).length; i++) {
                    formData.append('newsImgSrc', Object.values(files)[i]);
                }
                formData.append("newsImgSrcList", JSON.stringify(srcArr));
                $.ajax({
                    type: "POST",
                    url: config.base_server + "updataNews",
                    async: false,
                    data: formData,
                    processData: false,   //  告诉jquery不要处理发送的数据
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            alert("修改成功");
                            parent.layer.close(index); //再执行关闭   
                        } else {
                            alert(data.msg);
                        }
                    },
                    error() {
                        alert("文件太大")
                    }
                });
                return false;
            });

        });
    </script>


</body>

</html>